package com.hongying.lingtianxiang;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

public class PosterProduct1 {
    public static void main(String[] args) throws IOException {
        BufferedImage bg1 = ImageIO.read(new File("bg1.png"));
        BufferedImage bg2 = ImageIO.read(new File("bg2.png"));
        BufferedImage head = ImageIO.read(new File("head-1.png"));
        BufferedImage code = ImageIO.read(new File("qrcode.jpg"));

        // 绘制头像
        Graphics2D g1 = bg2.createGraphics();
        Ellipse2D.Double shape = new Ellipse2D.Double(60, 127, 167, 167);
        g1.setClip(shape);
        System.out.println(head.getWidth());
        System.out.println(head.getHeight());
        g1.drawImage(head, 60, 127, 167, 167, null);
        g1.setClip(null);

        // 绘制昵称
        Font font = new Font("宋体", Font.BOLD, 40);
        g1.setFont(font);
        g1.setColor(new Color(66, 66, 66));
        // 设置昵称居中
        String text = "一天到晚游泳的鱼";
        g1.drawString(text, 282, 229);

        // 绘制邀请规则
        font = new Font("宋体", Font.PLAIN, 35);
        g1.setFont(font);
        g1.setColor(new Color(88, 88, 88));
        text = "邀请规则";
        g1.drawString(text, 68, 421);

        font = new Font("宋体", Font.PLAIN, 30);
        g1.setFont(font);
        text = "生活赋予我们一种巨大的和无限高贵的礼品，这就是青春：充满着力量，充满着期待志愿，充满着求知和斗争的志向，充满着希望信心和青春。";
        drawStringWithFontStyleLineFeed(g1, text, 68, 491, font);

        // 绘制虚线
        float[] dash1 = {2f, 0f, 2f};
        BasicStroke bs1 = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1.0f, dash1, 2f);
        g1.setStroke(bs1);
        g1.setColor(new Color(229, 229, 229));
        g1.drawLine(50, 849, 850, 849);

        // 绘制长按识别二维码文字
        font = new Font("宋体", Font.PLAIN, 35);
        g1.setFont(font);
        g1.setColor(new Color(88, 88, 88));
        text = "长按识别二维码";
        g1.drawString(text, 64, 1068);

        // 绘制二维码图片
        System.out.println(code.getWidth() / 2);
        System.out.println(code.getHeight() / 2);
        g1.drawImage(code, 610, 880, 235, 267, null);

        g1.dispose();

        // 绘制图片
        Graphics2D g2 = bg1.createGraphics();
        g2.drawImage(bg2, 112, 336, bg2.getWidth(), bg2.getHeight(), null);
        g2.dispose();

        ImageIO.write(bg1, "png", new File("C:\\Users\\qwer\\Desktop\\a\\poster.png"));
    }

    // 获取网络图片
    public BufferedImage getUrlImg(String urlImg) {
        BufferedImage img = null;
        try {
            URL url = new URL(urlImg);
            InputStream is = url.openConnection().getInputStream();
            img = ImageIO.read(is);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return img;
    }

    //java画图时切割字符串，font就一行显示多少字=控件宽度/字体大小-首尾空格（width/fontSize - 2）

    //字符串总宽度
    private static int getStringLength(Graphics g, String str) {
        char[] strcha = str.toCharArray();
        int strWidth = g.getFontMetrics().charsWidth(strcha, 0, str.length());
        System.out.println("字符总宽度:" + strWidth);
        return strWidth;
    }

    //每一行字符的个数
    private static int getRowStrNum(int strnum, int rowWidth, int strWidth) {
        int rowstrnum = 0;
        rowstrnum = (rowWidth * strnum) / strWidth;
        System.out.println("每行的字符数:" + rowstrnum);
        return rowstrnum;
    }

    //字符行数
    private static int getRows(int strWidth, int rowWidth) {
        int rows = 0;
        if (strWidth % rowWidth > 0) {
            rows = strWidth / rowWidth + 1;
        } else {
            rows = strWidth / rowWidth;
        }
        System.out.println("行数:" + rows);
        return rows;
    }

    //字符高度
    private static int getStringHeight(Graphics g) {
        int height = g.getFontMetrics().getHeight();
        System.out.println("字符高度:" + height);
        return height;
    }

    private static void drawStringWithFontStyleLineFeed(Graphics g, String strContent, int loc_X, int loc_Y, Font font) {
        g.setFont(font);
        //获取字符串 字符的总宽度
        int strWidth = getStringLength(g, strContent);
        //每一行字符串宽度
        int rowWidth = 764;
        System.out.println("每行字符宽度:" + rowWidth);
        //获取字符高度
        int strHeight = getStringHeight(g);
        //字符串总个数
        System.out.println("字符串总个数:" + strContent.length());
        if (strWidth > rowWidth) {
            int rowstrnum = getRowStrNum(strContent.length(), rowWidth, strWidth);
            int rows = getRows(strWidth, rowWidth);
            String temp = "";
            for (int i = 0; i < rows; i++) {
                //获取各行的String
                if (i == rows - 1) {
                    //最后一行
                    temp = strContent.substring(i * rowstrnum, strContent.length());
                } else {
                    temp = strContent.substring(i * rowstrnum, i * rowstrnum + rowstrnum);
                }
                if (i > 0) {
                    //第一行不需要增加字符高度，以后的每一行在换行的时候都需要增加字符高度
                    loc_Y = loc_Y + strHeight;
                }
                g.drawString(temp, loc_X, loc_Y);
            }
        } else {
            //直接绘制
            g.drawString(strContent, loc_X, loc_Y);
        }

    }
}
